package org.restoran.controller.web;

import org.restoran.model.Order;
import org.restoran.service.OrderService;
import org.restoran.service.TableService;
import org.restoran.util.Utills;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 * User: Andrew Kononenko
 * Date: 18.11.13
 * Time: 23:53
 */

@Controller
@RequestMapping(value = "/order", method = RequestMethod.GET, consumes = {"text/plain", "application/*"})
public class OrderController extends AbstractController {

    private static Logger log = LoggerFactory.getLogger(OrderController.class);

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody String allExistNow() {
        log.debug("Request to all exist now orders");

        String s = orderService.parseToJson(Utills.collection(orderService.findAll()));
        return success(s);
    }

    @RequestMapping(value = "/{orderId}", method = RequestMethod.GET)
    public @ResponseBody String findeById(
            @PathVariable("orderId")
            Integer id)
    {
        Order order = orderService.findOne(id);

        String json = orderService.parseToJson(order);
        log.debug("return json response {}", json);
        return success(json);
    }

    @RequestMapping (value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<String> updateOne(
            @RequestBody
            String json)
    {
        Order order = orderService.parseFromJson(json, Order.class);

        log.debug("deserializer object {} from JSON {}, and try update", order, json);

        Order updated = orderService.save(order);
        String respJson = orderService.parseToJson(updated);

        return new ResponseEntity<>(success(respJson), HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public  ResponseEntity<String> addOne(
            @RequestBody
            String json)
    {
        Order parsetOrder = orderService.parseFromJson(json, Order.class);

        log.debug("deserializer object {} from JSON {}, and try update", parsetOrder, json);

        Order created = orderService.save(parsetOrder);
        String result = orderService.parseToJson(created);

        return new ResponseEntity<>(success(result), HttpStatus.ACCEPTED);
    }



    @RequestMapping(value = "/{orderId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(
            @PathVariable("orderId")
            Integer orderId)
    {
        orderService.delete(orderId);
        return new ResponseEntity<>(success(), HttpStatus.OK);
    }

}
















